package com.hxs.colorfultest2.controller;

import com.hxs.colorfultest2.domain.Factory;
import com.hxs.colorfultest2.domain.User;
import com.hxs.colorfultest2.model.ResponseModel;
import com.hxs.colorfultest2.service.FactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Controller
public class FactoryManagerController {
    @Autowired
    FactoryService factoryService;

    /*获取所有用户进行分页*/
    @RequestMapping("/factory")
    public Model getAllFactory(Model model, @RequestParam(value = "curPage", required = false) Integer curPage) {
        int pageSize = 10;

        /*当前页*/
        if (curPage == null) {
            curPage = 1;
        }
        int totalRows = factoryService.getFactoryByTotal();
        int totalPages = totalRows / pageSize;
        int left = totalRows % pageSize;
        if (left > 0) {
            totalPages = totalPages + 1;
        }

        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        /*计算当前页*/
        int start = (curPage - 1) * pageSize;
        paramMap.put("start", start);
        paramMap.put("pageSize", pageSize);

        List<Factory> factoryList = factoryService.getFactoryByPage(paramMap);
        new ResponseModel<List<Factory>>(0, "success", factoryList);
        model.addAttribute("factoryList", factoryList);
        model.addAttribute("totalRows", totalRows);
        model.addAttribute("curPage", curPage);
        model.addAttribute("totalPages", totalPages);
        model.addAttribute("pageSize", pageSize);

        List<Factory> del_factoryList = factoryService.getDeleteFactory();
        model.addAttribute("del_factoryList", del_factoryList);

      /*  for (Factory factory : factoryList) {
            System.out.println(factory);
        }*/

        return model;
    }

    /*厂商名查重*/
    @RequestMapping(value = "/factoryCheck", method = RequestMethod.GET)
    public void nameCheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        boolean b = factoryService.checkName(name);

        if (b) {
                   response.getOutputStream().print("1");
            // return new ResponseModel<Boolean>(0,"可用",true);
        } else {
            // return new ResponseModel<Boolean>(-1,"不可用",false);
            response.getOutputStream().print("0");
        }
    }

    /*保存新增或者更新的的厂商*/
    @RequestMapping(value = "/addF", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel<Boolean> saveU(HttpServletRequest request,HttpServletResponse response) throws IOException {

        String name=request.getParameter("name");
        String id=request.getParameter("ID");
        if(id==null||id=="") {
            Factory factory = new Factory();
            System.out.println("输入框的厂商名为" + name);
            factory.setName(name);
            factoryService.addFactory(factory);

            return new ResponseModel<Boolean>(0, "添加成功", true);
        }else{
            Factory factory=factoryService.getFactoryByID(Integer.valueOf(id));
            factory.setName(name);
            factoryService.updateFactory(factory);
            return new ResponseModel<Boolean>(0, "更新成功", true);
        }
    }

    /*删除厂商*/
    @RequestMapping(value = "/deleteF{name}", method = RequestMethod.GET)
    public String deleteU(@PathVariable("name") String name) throws IOException {
        System.out.println("要删除的厂商名为" + " " + name);
        factoryService.deleteFactory(name);
        return "redirect:/factory";


    }

    /*恢复删除厂商*/
    @RequestMapping(value = "/rebackF{name}", method = RequestMethod.GET)
    public String rebackF(@PathVariable("name") String name) throws IOException {
        System.out.println("要恢复的厂商名为" + " " + name);
        Factory factory=factoryService.getFactoryByName(name);
        factory.setStatus(0);
        factoryService.updateFactory(factory);
        return "redirect:/factory";


    }

    /*获得更新的厂商*/
    @RequestMapping(value = "/getF", method = RequestMethod.GET)
    public void getF(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        System.out.println("要更行的厂商ID为" + " " + id);
        Factory factory = factoryService.getFactoryByID(Integer.valueOf(id));
        System.out.println("要更行的厂商为" + factory);

        response.getOutputStream().print(factory.getName());

    }

   /* *//*保存更新的厂商*//*
    @RequestMapping(value = "/updateF", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel<Boolean> updateF(@RequestParam("name") String name) throws IOException {
         Factory factory=factoryService.getFactoryByName(name);
        factoryService.updateFactory(factory);

        return new ResponseModel<Boolean>(0, "更新成功", true);
    }*/

    @ResponseBody
    @RequestMapping("/getFactorys")
    public List getFactorys(HttpServletRequest request,HttpServletResponse response){
        System.err.println("接收到了要获取所有厂商的请求");
        List<Factory> factoryList=factoryService.getAllFactory();
        return factoryList;

    }




}
